package com.nvn.washiodriver.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.Order;
import com.nvn.washiodriver.Objects.OrderStatus;
import com.nvn.washiodriver.adapters.OrderListAdapter;
import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.network.ws.BaseWSControl;
import com.nvn.washiodriver.network.ws.BaseWSControl.WebServiceFlag;
import com.nvn.washiodriver.network.ws.GetOrderListWSControl;
import com.nvn.washiodriver.network.ws.GetStatusesListWSControl;
import com.nvn.washiodriver.utils.SessionManager;
import com.parse.ParseUser;

public class OrderActivity extends BaseActivity implements
		WebServiceCommunicatorListener {

	public static int REQUEST_CODE_NOTE = 111;
	public static int REQUEST_CODE_DETAIL = 112;
	private boolean getOrderOk, getStatusOk;
	private ListView mOrderList;
	private OrderListAdapter mAdapter;
	private ArrayList<Order> mOrders;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orders);

		initView();
		initProperties();
		addListeners();

	}

	@Override
	protected void initView() {
		super.initView();

		mOrderList = (ListView) findViewById(R.id.list_orders);

		mAdapter = new OrderListAdapter(this, new ArrayList<Order>());
	}

	@SuppressLint("SimpleDateFormat")
	private void initProperties() {
		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Loading...");
		mOrderList.setAdapter(mAdapter);
		
		Date d = new Date();
		String curDateTime = new SimpleDateFormat(
				"dd-MM-yy").format(d);
		mTvTitle.setText("Orders: "+curDateTime);
		mImvTitle.setImageResource(R.drawable.button_back);
		
		loadData();
	}

	public void loadData() {
		getOrderOk = false;
		getStatusOk = false;

		String userId = ParseUser.getCurrentUser().getObjectId();
		new GetOrderListWSControl(this, this, userId).execute();
		if (SessionManager.getSessionManager(this).getoStatuses().size() == 0) {
			new GetStatusesListWSControl(this, this, userId).execute();
		} else {
			getStatusOk = true;
		}
	}

	private void addListeners() {

		mOrderList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Order o = mOrders.get(arg2);
				SessionManager.getSessionManager(OrderActivity.this)
						.setCurOrder(o);

				Intent i = new Intent(OrderActivity.this,
						OrderDetailActivity.class);
				i.putExtra("FIRST_ORDER", arg2==0);
				startActivityForResult(i, REQUEST_CODE_DETAIL);
			}
		});
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {

		pd.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		Toast.makeText(this, error, Toast.LENGTH_SHORT).show();

		if (flag == WebServiceFlag.GET_ORDER) {
			getOrderOk = true;
		}

		if (flag == WebServiceFlag.GET_ORDER_STATUSES) {
			getStatusOk = true;
		}

		if (getOrderOk && getStatusOk) {
			pd.dismiss();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		if (flag == WebServiceFlag.GET_ORDER) {

			mOrders = (ArrayList<Order>) result;

			if (mOrders.size() > 0) {
				mOrderList.setVisibility(View.VISIBLE);
				mAdapter.setData(mOrders);
			}

			getOrderOk = true;
		}

		if (flag == WebServiceFlag.GET_ORDER_STATUSES) {
			SessionManager.getSessionManager(OrderActivity.this).setoStatuses(
					(ArrayList<OrderStatus>) result);
			getStatusOk = true;
		}

		if (getOrderOk && getStatusOk) {
			pd.dismiss();
		}
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			new GetOrderListWSControl(this, this, ParseUser.getCurrentUser()
					.getObjectId()).execute();
		}
	}

}
